	package com.xnx3.wangmarket.plugin.tcdnAutoGenerate.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.pluginManage.controller.BasePluginController;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.util.SessionUtil;
import com.xnx3.wangmarket.plugin.tcdnAutoGenerate.entity.Tcdn;
import com.xnx3.wangmarket.plugin.tcdnAutoGenerate.vo.TcdnVO;

/**
 * tcdn插件
 * @author 管雷鸣
 */
@Controller(value="tcdnAutoGeneratePluginController")
@RequestMapping("/plugin/tcdn/")
public class TCDNPluginController extends BasePluginController{
	@Resource
	private SqlService sqlService;
	
	/**
	 * 后台首页
	 */
	@ResponseBody
	@RequestMapping("index.json")
	public TcdnVO index(HttpServletRequest request){
		TcdnVO vo = new TcdnVO();
		if(!haveSiteAuth()){
			vo.setBaseVO(TcdnVO.FAILURE, "尚未登录");
			return vo;
		}
		
		Site site = SessionUtil.getSite();
		Tcdn kefu = sqlService.findById(Tcdn.class, site.getId());
		if(kefu == null){
			// 如果用户未设置，第一次用，那么初始化,创建一个
			kefu = new Tcdn();
			kefu.setSiteid(site.getId());
			kefu.setIsUse(Tcdn.ISUSE_FALSE);	//默认不启用
			sqlService.save(kefu);
			
		}
		vo.setTcdn(kefu);
		
		ActionLogUtil.insert(request, site.getId(), "进入tcdn首页");
		return vo;
	}
	
	/**
	 * 更改当前网站，是否使用tcdn
	 * @param use 是否使用，1使用，0不使用
	 */
	@RequestMapping(value="useTcdn.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO useTcdn(HttpServletRequest request,
			@RequestParam(value = "use", required = false , defaultValue="0") short use){
		if(!haveSiteAuth()){
			return error("无权使用");
		}
		
		Site site = SessionUtil.getSite();
		Tcdn kefu = sqlService.findById(Tcdn.class, site.getId());
		kefu.setIsUse(use - Tcdn.ISUSE_TRUE == 0? Tcdn.ISUSE_TRUE:Tcdn.ISUSE_FALSE);
		sqlService.save(kefu);
		
		//记录日志
		ActionLogUtil.insert(request, site.getId(), (use - Tcdn.ISUSE_TRUE == 0 ? "开启":"关闭")+"插件的tcdn");
		return success();
	}
	
	
	/**
	 * 保存tcdn自动触发翻译的URL
	 * @param url 生成整站后自动请求的url,格式如 http://xxxxx.com/xxxxx/generate.json?xxx=xx  如果为空则是不启用
	 */
	@RequestMapping(value="saveTcdn.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO saveTcdn(HttpServletRequest request,
			@RequestParam(value = "url", required = false , defaultValue="") String url){

		if(!haveSiteAuth()){
			return error("无权使用");
		}
		
		if(!StringUtils.hasLength(url)){
			return error("请输入URL");
		}
		
		Site site = SessionUtil.getSite();
		Tcdn kefu = sqlService.findById(Tcdn.class, site.getId());
		if (kefu == null) {
			return error("当前网站的Tcdn不存在");
		}
		kefu.setUrl(url);
		sqlService.save(kefu);
		
		//记录日志
		ActionLogUtil.insert(request, site.getId(), "保存tcdn自动触发翻译的URL: " + kefu.toString());
		return success();
	}
	
}
